package com.lagou.controller;

import com.lagou.model.User;
import com.lagou.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSessionEvent;
import java.lang.management.ManagementFactory;
import java.util.Set;


@Controller
public class LoginController {
    @Autowired
    private UserService userService;
    @RequestMapping("/login")
    public ModelAndView loginView(String username, String password, HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView("loginView");
        modelAndView.addObject("localPort", request.getLocalPort());
        modelAndView.addObject("sessionId", request.getSession().getId());
        User user = userService.login(username, password);
        if (user == null) {
            return modelAndView;
        } else {
            request.getSession().setAttribute("username", username);
            modelAndView.setViewName("index");
            return modelAndView;
        }
    }
}
